package com.neusoft.movie.v_movies.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.adpater.CommentListAdapter;
import com.neusoft.movie.v_movies.common.Constants;
import com.neusoft.movie.v_movies.javabean.CommentBean;
import com.neusoft.movie.v_movies.javabean.NewsBean;
import com.neusoft.movie.v_movies.javabean.ResultBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.CommentsModel;
import com.neusoft.movie.v_movies.model.MoviesModel;
import com.neusoft.movie.v_movies.model.NewsModel;
import com.neusoft.movie.v_movies.model.UsersModel;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.neusoft.movie.v_movies.common.Constants.PHOTO_URL;

public class NewsInfoActivity extends BaseActivity implements RetrofitListener {
    private RecyclerView rv;
    private CommentListAdapter commentListAdapter;
    private TextView n_name,n_time,n_info,n_address;
    private ImageView n_pic,left,button_comment;
    private int n_id,comment_flag,comment_id;
    private Context mContext;
    private String result;
    private int u_id;
    private EditText com_content;
    @Override
    void initViews() {
        setLayout(R.layout.activity_news_info);

        rv = (RecyclerView) findViewById(R.id.rv_comment);
        rv.setLayoutManager(new LinearLayoutManager(this));

        n_name = (TextView) findViewById(R.id.n_name);
        n_pic = (ImageView) findViewById(R.id.n_pic);
        n_time = (TextView) findViewById(R.id.n_time);
        n_address = (TextView) findViewById(R.id.n_address);
        n_info = (TextView) findViewById(R.id.n_info);

        com_content= (EditText) findViewById(R.id.com_content);

        button_comment=(ImageView) findViewById(R.id.button_comment);
        left=(ImageView) findViewById(R.id.left_button);

    }

    @Override
    void initData() {
        n_id=getIntent().getIntExtra("n_id",-1);
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id=sharedPreferences.getInt("u_id",0);

        getDetail();
        getCommentlist();
    }
    private void getDetail()
    {
        NewsModel newsModel = new NewsModel();
        newsModel.getDetail(n_id,this);
    }
    private void getCommentlist()
    {
        CommentsModel commentsModel = new CommentsModel();
        commentsModel.getCommentlist(n_id,2,this);
    }


    private void addcomments()
    {
        UsersModel usersModel = new UsersModel();
        usersModel.addcomments(u_id, n_id, 2, com_content.getText().toString(),this);

    }

    @Override
    void initEvents() {
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        button_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addcomments();
            }
        });

    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch(flag){
            case Constants.GETNEWDETAIL:
                NewsBean newsBean=(NewsBean) o;
                String pic = PHOTO_URL+newsBean.getN_pic();
                if(!newsBean.getN_pic().equals("")){
                    Picasso.with(mContext).load(pic).into(n_pic);
                }

                n_name.setText(newsBean.getN_name());
                n_info.setText(newsBean.getN_info());
                n_address.setText(newsBean.getN_address());
                n_time.setText(newsBean.getN_time());
                break;

            case Constants.GETMOVIESCOMMENTLIST:
                List<CommentBean> list=(List<CommentBean>)o;
                CommentListAdapter adapter=new CommentListAdapter(this,list);
                rv.setAdapter(adapter);
                break;


            case Constants.ADDCOMMENTS:
                if (((ResultBean)o).getSuccess().equals("1")) {
                    Toast.makeText(NewsInfoActivity.this, "评论成功", Toast.LENGTH_LONG).show();
                    finish();
                } else {
                    Toast.makeText(NewsInfoActivity.this, "评论失败", Toast.LENGTH_LONG).show();

                }
        }
    }

    @Override
    public void onFailed() {

    }
}
